package codeTop;

import java.util.Stack;

public class q227 {
    public int calculate1(String s) {
        Stack<Integer> intStack = new Stack<>();
        Stack<Character> charStack = new Stack<>();
        for (int i = 0; i <s.length(); i++) {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                int num = 0;
                while (i<s.length()&&s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                    num = num*10+s.charAt(i)-'0';
                    i++;
                }
                i--;
                intStack.push(num);
            } else if (s.charAt(i) == '+' || s.charAt(i) == '-') {
                while ((!charStack.isEmpty())&&(charStack.peek()=='*'||charStack.peek()=='/')) {
                    Character c = charStack.pop();
                    Integer pop1 = intStack.pop();
                    Integer pop2 = intStack.pop();
                    if (c == '*') {
                        intStack.push(pop2*pop1);
                    }else {
                        intStack.push(pop2/pop1);
                    }
                }
                while ((!charStack.isEmpty())&&(charStack.peek()=='+'||charStack.peek()=='-')) {
                    Character c = charStack.pop();
                    Integer pop1 = intStack.pop();
                    Integer pop2 = intStack.pop();
                    if (c == '+') {
                        intStack.push(pop2+pop1);
                    }else {
                        intStack.push(pop2-pop1);
                    }
                }
                charStack.push(s.charAt(i));
            }else if (s.charAt(i) == '*' || s.charAt(i) == '/'){
                while ((!charStack.isEmpty())&&(charStack.peek()=='*'||charStack.peek()=='/')) {
                    Character c = charStack.pop();
                    Integer pop1 = intStack.pop();
                    Integer pop2 = intStack.pop();
                    if (c == '*') {
                        intStack.push(pop2*pop1);
                    }else {
                        intStack.push(pop2/pop1);
                    }
                }
                charStack.push(s.charAt(i));
            }
        }
        while (!charStack.isEmpty()) {
            Character c = charStack.pop();
            Integer pop1 = intStack.pop();
            Integer pop2 = intStack.pop();
            if (c == '+') {
                intStack.push(pop2+pop1);
            }else if (c == '-') {
                intStack.push(pop2-pop1);
            } else if (c == '*') {
                intStack.push(pop2*pop1);
            }else {
                intStack.push(pop2/pop1);
            }
        }
        return intStack.pop();
    }
    public int calculate(String s) {
        Stack<Integer> stack = new Stack<>();
        char preSign = '+';
        int idx = 0;
        int num = 0;
        while (idx <= s.length()) {
            if (idx<s.length()&&Character.isDigit(s.charAt(idx))) {
                num = num*10+s.charAt(idx)-'0';
                idx++;
            } else if (idx<s.length()&&s.charAt(idx)==' ') {
                idx++;
            } else {
                switch (preSign){
                    case '+':stack.push(num);break;
                    case '-':stack.push(-num);break;
                    case '*':stack.push(stack.pop()*num);break;
                    case '/':stack.push(stack.pop()/num);break;
                }
                preSign = idx==s.length()?' ':s.charAt(idx);
                idx++;
                num = 0;
            }
        }
        int res = 0;
        while (!stack.isEmpty()) {
            res+=stack.pop();
        }
        return res;
    }

    public static void main(String[] args) {
        q227 q = new q227();
        System.out.println(q.calculate("3+2*2"));
    }
}
